/**
 * All rights Reserved, Designed By 溪云阁
 * Copyright:    Copyright(C) 2016-2020
 */

package com.module.boots.coder.business;

import java.sql.SQLException;

import org.apache.velocity.VelocityContext;

import com.module.boots.coder.base.BaseCoder;
import com.module.boots.coder.utils.MysqlBeanUtils;
import com.module.boots.coder.utils.PageUtils;

/**
 * 业务逻辑代码生成器
 * @author：溪云阁
 * @date：2020年9月5日
 */

public class BusinessCoder extends BaseCoder {

    /**
     * 生成XML文件
     * @author 溪云阁
     * @throws SQLException void
     */
    public void createXML() throws Exception {
        // 获取最基础的模板
        final VelocityContext context = getBaseContext(tableName);
        // xml的路径
        final String xmlPath = getBaseXMLPath() + getUpperClassName(tableName) + "Mapper.xml";
        PageUtils.WriterPage(context, "business/xml.ftl", xmlPath);
    }

    /**
     * 生成实体类
     * @author 溪云阁
     * @throws SQLException void
     */
    public void createEntity() throws Exception {
        // 获取最基础的模板
        final VelocityContext context = getBaseContext(tableName);
        // 实体类的路径
        final String entityPath = getBasePackagePath() + "/business/" + getUpperClassName(tableName).toLowerCase() + "/model/" + getUpperClassName(tableName) + ".java";
        context.put("feilds", MysqlBeanUtils.getEntityFeilds(tableName));
        context.put("importTypes", MysqlBeanUtils.getEntityPacking(tableName));
        PageUtils.WriterPage(context, "business/entity.ftl", entityPath);
    }

    /**
     * 生成dao类
     * @author 溪云阁
     * @throws SQLException void
     */
    public void createDao() throws Exception {
        // 获取最基础的模板
        final VelocityContext context = getBaseContext(tableName);
        // 数据类的路径
        final String daoPath = getBasePackagePath() + "/business/" + getUpperClassName(tableName).toLowerCase() + "/dao/" + "I" + getUpperClassName(tableName) + "Dao.java";
        PageUtils.WriterPage(context, "business/dao.ftl", daoPath);
    }

    /**
     * 生成service类
     * @author 溪云阁
     * @throws SQLException void
     */
    public void createService() throws Exception {
        // 获取最基础的模板
        final VelocityContext context = getBaseContext(tableName);
        // 服务类的路径
        final String servicePath = getBasePackagePath() + "/business/" + getUpperClassName(tableName).toLowerCase() + "/service/" + "I" + getUpperClassName(tableName) + "Service.java";
        PageUtils.WriterPage(context, "business/service.ftl", servicePath);
    }

    /**
     * 生成serviceimpl类
     * @author 溪云阁
     * @throws SQLException void
     */
    public void createServiceimpl() throws Exception {
        // 获取最基础的模板
        final VelocityContext context = getBaseContext(tableName);
        // 服务实现类的路径
        final String serviceimplPath = getBasePackagePath() + "/business/" + getUpperClassName(tableName).toLowerCase() + "/service/impl/" + getUpperClassName(tableName) + "ServiceImpl.java";
        PageUtils.WriterPage(context, "business/serviceimpl.ftl", serviceimplPath);
    }

}
